<%-- 
Copyright [2008] [Manuel Kraft] 
 Licensed under the Apache License, Version 2.0 (the "License");
 you may not use this file except in compliance with the License.
 You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
 Unless required by applicable law or agreed to in writing, software distributed under
 the License is distributed on an
 "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 See the License for the specific language governing permissions and limitations under the License.

Author     : cutmasta@gmail.com
--%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">

<%@ page import="org.cutmasta.ip.*" %>
<%@ page import="org.apache.commons.httpclient.*" %>
<%@ page import="org.apache.commons.httpclient.methods.*" %>
<%@ page import="org.json.*" %>

<%@ page import=" org.apache.commons.httpclient.protocol.Protocol"%>
<%@ include file="authcheck.jsp" %>


<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <link rel="stylesheet" type="text/css" href="mystyle.css"/>
        <title>..::..</title>
    </head>
    <body>
        
        <%

            User s = (User) request.getSession().getAttribute("user");
            if (s == null) {
                out.println("Auth against OX failed!Please try again!<br><br>");
                out.println("<a href=\""+response.encodeURL("login.jsp")+"\">Here</a>");
            } else {
                // login new to ox            
                if (s.getSessionObject() == null) {
                    // try to fetch data via httpclient and json api 

                    Protocol easyhttps = new Protocol("https", new EasySSLProtocolSocketFactory(), 443);
                    Protocol.registerProtocol("https", easyhttps);

                    HttpClient client = new HttpClient();

                    PostMethod login_post = new PostMethod("https://" + s.getOxurl() + "/ajax/login?action=login");
                    login_post.addParameter("name", s.getUsername());
                    login_post.addParameter("password", s.getPassword());

                    try {

                        client.executeMethod(login_post);

                        JSONObject loginObject = ResponseTools.reponse2JsonObject(login_post);

                        if (loginObject.has("session")) {
                            SessionObject so = new SessionObject();
                            so.setCookies(client.getState().getCookies());
                            so.setSessionid((String) loginObject.get("session"));
                            so.setOxClient(client);
                            s.setSessionObject(so);

                            // redirect again so that he can see his data on this page
                            response.sendRedirect(response.encodeRedirectURL("index.jsp"));
                        } else {
                            out.println("Auth against OX failed!Please try again!<br><br>");
                            out.println("<a href=\""+response.encodeURL("login.jsp")+"\">Here</a>");
                        }

                    } catch (Exception exp) {
                        exp.printStackTrace();
                    } finally {
                        login_post.releaseConnection();
                    }

                } else {
                    response.sendRedirect(response.encodeRedirectURL("beta.jsp?module=calendar"));
                }
            }  
        
        %>
        
        
    </body>
</html>
